package com.iplay.实战java高并发程序设计.chapter5.c55;

import com.google.common.util.concurrent.*;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;

/**
 * @author dangwu
 * @since 2021/11/5 6:38 下午
 */
public class GuavaFutureMain {
    public static void main(String[] args) throws InterruptedException {
        ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
        ListenableFuture futureTask = listeningExecutorService.submit(new RealData2("x"));
//        futureTask.addListener(() ->{
//            System.out.println("异步处理成功：");
//            try {
//                System.out.println(futureTask.get());
//            } catch (InterruptedException | ExecutionException e) {
//                e.printStackTrace();
//            }
//        },MoreExecutors.directExecutor());
        Futures.addCallback(futureTask, new FutureCallback<String>() {

            @Override
            public void onSuccess(@Nullable String s) {
                System.out.println("异步处理成功，result="+s);
            }

            @Override
            public void onFailure(Throwable throwable) {
                System.out.println("异步处理失败，e="+throwable);
            }
        },MoreExecutors.newDirectExecutorService());


        System.out.println("main task done......");
        Thread.sleep(3000);
    }
}
